<?php
//============================ CONSTANSTS ======================================$LAST_POST = array ( ) ;
//$PDD_DIR = 'D:\\www\\www\\BRO\\PDF\\';//============================ INIT ============================================set_time_limit ( 3600 * 24 * 2 ) ; //maksimalus vykdymo laikotarpis = nurodytas +30serror_reporting ( E_ALL ^ E_NOTICE ) ;
ini_set ( 'display_errors', 1 ) ;
ini_set ( "memory_limit", "200M" ) ;
ini_set ( 'include_path', get_include_path () . PATH_SEPARATOR . 'D:/www/www/libraries/DB/' ) ;
ini_set ( 'include_path', get_include_path () . PATH_SEPARATOR . 'D:/www/www/BRP/' ) ;
ini_set ( 'include_path', get_include_path () . PATH_SEPARATOR . 'D:/www/www/libraries/DB/' ) ;
ini_set ( 'include_path', get_include_path () . PATH_SEPARATOR . 'D:/www/php/PEAR/' ) ;
//require_once ("/www/www/hotstocked.com/include/settings.php");//require_once ("/www/www/libraries/cdb.php");//$klase = new CDB();//============================ MAIN ============================================$ii = 0 ;
if (count ( $_POST ) > 0) {
	$query_word = 'example' ;
	$query_word2_array = getNames ( $klase, 6168, 1000 ) ;
	foreach ( $query_word2_array as $query_word2 ) {
		
		sleep ( 2 ) ;
		echo str_pad ( '', 4096 ) . "\n" ;
		ob_flush () ;
		flush () ;
		
		unset ( $LAST_POST ) ;
		$POST2 = $_POST ;
		foreach ( $POST2 as $key => $value ) {
			if ($value == $query_word) {
				$POST2 [ $key ] = $query_word2 ;
			}
		}
		//read list		getFileWithKuki ( 'http://brokercheck.finra.org/Search/Search.aspx?PageID=1', $POST2 ) ;
		$POST2 [ '__EVENTTARGET' ] = 'ctl00$phContent$navPager$lb1' ;
		$s = getFileWithKuki ( 'http://brokercheck.finra.org/Search/SearchResults.aspx' ) ;
		if (strpos ( $s, '<h2>Object moved to <a href="/Support/NoResults.aspx">here' ) === false) {
			parseList ( $s, $klase ) ;
			
			//get total pages			$pages = 0 ;
			$pages = GetBetween ( $s, 'style="font-weight:normal;">Results ', 'span>' ) ;
			$pages = GetBetween ( $pages, 'of ', '<' ) ;
			$pages = floor ( $pages / 15 ) ;
			
			$POST2 [ '__EVENTTARGET' ] = 'ctl00$phContent$navPager$lb1' ;
			$POST2 [ '__EVENTARGUMENT' ] = '' ;
			$POST2 [ '__VIEWSTATE' ] = GetBetween ( $s, '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="', '"' ) ;
			$POST2 [ '__EVENTVALIDATION' ] = GetBetween ( $s, '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="', '"' ) ;
			
			echo "[$pages]" ;
			
			//correction			if ($pages != 0 && $pages != 1) {
				//paging				for ( $j = 1 ; $j < $pages + 2 ; $j ++ ) {
					$s2 = getNextPage ( $POST2, $j ) ; //echo $s2;					parseList ( $s2, $klase ) ;
				}
			}
		} else {
			//no results			echo 'n/a<br />' ;
		}
		$ii ++ ;
		echo "======================[$ii]====================================== $query_word2<br /\n>" ;
	}
} else {
	//show search form	echo '<base href="http://brokercheck.finra.org" />' ;
	
	echo getFileWithKuki ( 'http://brokercheck.finra.org/Search/Search.aspx?PageID=1' ) ;
}

//echo getFileWithKuki_2('http://brokercheck.finra.org/Support/TermsAndConditions.aspx');//echo getFileWithKuki('http://brokercheck.finra.org/Search/Search.aspx?PageID=1', 'ADAM');//echo getFileWithKuki2('http://brokercheck.finra.org/Search/SearchResults.aspx');//echo getFileWithKuki3('http://brokercheck.finra.org/Individual/IndividualSummary.aspx', 'ctl00$phContent$gvBrokerTable$ctl10$lnkName');//echo getFileWithKuki4('http://brokercheck.finra.org/Individual/IndividualSummary.aspx');//echo getFileWithKuki5('http://brokercheck.finra.org/Support/ReportViewer.aspx?IndvlCRD=719114');

//======================== FUNCTIONS ===========================================function getNextPage ( $POST , $nr ) {
	global $LAST_POST ;
	echo "[p:$nr]" ;
	
	$small_page = "$nr" ;
	$small_page = $small_page [ strlen ( $small_page ) - 1 ] ;
	if ($small_page == 0)
		$small_page = 10 ;
	
	if ($small_page == 1 && $nr != 1) {
		//this is another paging part		$LAST_POST [ '__EVENTTARGET' ] = substr ( $POST [ '__EVENTTARGET' ], 0, strlen ( $POST [ '__EVENTTARGET' ] ) - 1 ) . "NextSection" ;
		$content = getFileWithKuki ( 'http://brokercheck.finra.org/Search/SearchResults.aspx', $LAST_POST ) ;
		//saving some data		$LAST_POST [ '__EVENTARGUMENT' ] = '' ;
		$LAST_POST [ '__VIEWSTATE' ] = GetBetween ( $content, '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="', '"' ) ;
		$LAST_POST [ '__EVENTVALIDATION' ] = GetBetween ( $content, '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="', '"' ) ;
	} elseif ($nr > 10) {
		$LAST_POST [ '__EVENTTARGET' ] = substr ( $POST [ '__EVENTTARGET' ], 0, strlen ( $POST [ '__EVENTTARGET' ] ) - 1 ) . "$small_page" ;
		$content = getFileWithKuki ( 'http://brokercheck.finra.org/Search/SearchResults.aspx', $LAST_POST ) ;
		//saving some data		$LAST_POST [ '__EVENTARGUMENT' ] = '' ;
		$LAST_POST [ '__VIEWSTATE' ] = GetBetween ( $content, '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="', '"' ) ;
		$LAST_POST [ '__EVENTVALIDATION' ] = GetBetween ( $content, '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="', '"' ) ;
	} else {
		$POST [ '__EVENTTARGET' ] = substr ( $POST [ '__EVENTTARGET' ], 0, strlen ( $POST [ '__EVENTTARGET' ] ) - 1 ) . "$small_page" ;
		$content = getFileWithKuki ( 'http://brokercheck.finra.org/Search/SearchResults.aspx', $POST ) ;
		//saving some data		$LAST_POST [ '__EVENTARGUMENT' ] = '' ;
		$LAST_POST [ '__VIEWSTATE' ] = GetBetween ( $content, '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="', '"' ) ;
		$LAST_POST [ '__EVENTVALIDATION' ] = GetBetween ( $content, '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="', '"' ) ;
	}
	return $content ;
}
function getNames ( &$klase , $start , $n ) {
	$sql = "select officer_name
		from t_officers2
		order by officer_name asc
		limit $start, $n" ;
	return $klase->getCol ( $sql ) ;
}
function parseList ( $content_original , &$klase ) {
	if (strpos ( $content_original, 'FINRA BrokerCheck System is currently unavailable.' ) === false) {
	} else {
		echo "unavailable<br />" ;
		return false ;
	}
	if (strpos ( $content_original, 'Access Denied' ) === false) {
	} else {
		echo "<b>Access Denied !!! Proxy banned us. Sleeping</b><br />" ;
		sleep ( 60 ) ;
		echo '' ;
	}
	if (strpos ( $content_original, 'You are not authorized to view this page' ) === false) {
	} else {
		echo "<b>WE GOT BAN !!!</b><br />" ;
		die () ;
	}
	$content = GetBetween ( $content_original, 'Current Employing Brokerage Firms', '</table>' ) ;
	$parts_ = split ( '<tr ', $content ) ;
	$parts_ = array_slice ( $parts_, 1 ) ;
	foreach ( $parts_ as $part ) {
		unset ( $info ) ;
		$info [ 'name' ] = GetBetween ( $part, '\',\'\')">', ' <' ) ;
		$info [ 'EVENTTARGET' ] = GetBetween ( $part, 'javascript:__doPostBack(\'', '\'' ) ;
		//$info['EVENTARGUMENT'] = '';		$info [ 'VIEWSTATE' ] = GetBetween ( $content_original, '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="', '"' ) ;
		$info [ 'EVENTVALIDATION' ] = GetBetween ( $content_original, '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="', '"' ) ;
		$info [ 'CRD' ] = GetBetween ( $part, ' <br />(<b>', '<' ) ;
		$info [ 'names' ] = trim ( GetBetween ( $part, '</td><td align="left" valign="top" style="width:40%;">', '</td>' ) ) ;
		$info [ 'firms' ] = trim ( GetBetween ( $part, '</td><td valign="top" style="width:50%;">', '</td>' ) ) ;
		$info [ 'firms' ] = removeTags ( $info [ 'firms' ] ) ;
		
		$info [ 'name' ] = str_replace ( '  ', ' ', $info [ 'name' ] ) ;
		$info [ 'name' ] = str_replace ( '  ', ' ', $info [ 'name' ] ) ;
		
		insertBroker ( $info, $klase ) ;
		//parseIDPage($info);	}
}
function insertBroker ( $info , &$klase ) {
	if (trim ( $info [ 'name' ] ) == '') {
		echo "e<br />" ;
		return false ;
	}
	foreach ( $info as $key => $value )
		$info [ $key ] = addslashes ( $value ) ;
		//checking if exists	$sql = "select count(*)
		from t_brokers
		where broker_old_id = '$info[CRD]'
		limit 1" ;
	if ($klase->getOne ( $sql ) == 0) {
		$info [ 'name' ] = str_replace ( '&nbsp;', ' ', $info [ 'name' ] ) ;
		$info [ 'name' ] = str_replace ( '</a>', ' ', $info [ 'name' ] ) ;
		$info [ 'name' ] = str_replace ( '  ', ' ', $info [ 'name' ] ) ;
		$info [ 'name' ] = ucwords ( strtolower ( $info [ 'name' ] ) ) ;
		//insert		$sql = "insert into t_brokers(broker_old_id, broker_name, broker_names, broker_firms, broker_eventtarget, broker_viewstate, broker_eventvalidation, broker_date)
			values('$info[CRD]', '$info[name]', '$info[names]', '$info[firms]', '$info[EVENTTARGET]', '$info[VIEWSTATE]', '$info[EVENTVALIDATION]', NOW())" ;
		$klase->query ( $sql ) ;
		echo "+++<br />" ;
	} else {
		echo "-<br />" ;
	}
}
function parseIDPage ( $info ) {
	return false ;
	
	global $PDD_DIR ;
	$POSTAS = array ( '__EVENTTARGET' => $info [ 'EVENTTARGET' ] , '__EVENTARGUMENT' => '' , '__VIEWSTATE' => $info [ 'VIEWSTATE' ] , '__EVENTVALIDATION' => $info [ 'EVENTVALIDATION' ] ) ;
	getFileWithKuki ( 'http://brokercheck.finra.org/Search/SearchResults.aspx', $POSTAS ) ;
	$content = getFileWithKuki ( 'http://brokercheck.finra.org/Individual/IndividualSummary.aspx' ) ;
	echo $content ;
	
	$data [ 'broker_id' ] = GetBetween ( $content, 'CRD# ', '<' ) ;
	$data [ 'broker_name' ] = GetBetween ( $content, '<span id="ctl00_lblPageHeader" class="PageHeader">', '<' ) ;
	if ($data [ 'broker_id' ] == $info [ 'CRD' ])
		$data [ 'broker_names' ] = $info [ 'names' ] ;
	$data [ 'broker_firms' ] = GetBetween ( $content, 'Currently employed by and registered with the following FINRA firms:', 'Report Summary for this Broker' ) ;
	//$data['broker_firms'] = removeTags($data['broker_firms']);	$data [ 'broker_qualifications' ] = GetBetween ( $content, '<div id="ctl00_phContent_pnlRegistrations">', '</tr>' ) ;
	//$data['broker_qualifications'] = removeTags($data['broker_qualifications']);	$data [ 'broker_passed' ] = GetBetween ( $content, 'ctl00_phContent_pnlExams', '</tr>' ) ;
	//$data['broker_qualifications'] = removeTags($data['broker_qualifications']);	$data [ 'broker_suspended' ] = GetBetween ( $content, 'xxxxxxxx', 'xxxxxxx' ) ;
	$data [ 'broker_disclosed' ] = GetBetween ( $content, 'xxxxxxxx', 'xxxxxxx' ) ;
	
	die () ;
	
	//get pdf	getFileWithKuki ( 'http://brokercheck.finra.org/support/ReportStatus.aspx?PageID=1' ) ;
	$pdf = getFileWithKuki ( 'http://brokercheck.finra.org/Support/ReportViewer.aspx?IndvlCRD=' . $CRD ) ;
	
	if (strpos ( $pdf, '<h2>Object moved to <a href="/Search/Search.aspx?PageID=1">here</a>' ) === false) {
		echo WriteFile ( $PDD_DIR . $info [ 'CRD' ] . '.pdf', $pdf, "w" ) ;
	} else {
		die ( 'SESSION TIME OUT' ) ;
	}
	
	if (rand ( 1, 3 ) == 3)
		die ( '+' ) ;
}
function getFileWithKuki ( $url , $POST = false ) {
	$COOKIE_FILE_PATH = 'D:\\www\\www\\BRO\\cc.txt' ;
	$ch = curl_init () ;
	curl_setopt ( $ch, CURLOPT_URL, $url ) ;
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 ) ;
	//curl_setopt ($ch, CURLOPT_COOKIEJAR, $COOKIE_FILE_PATH);	curl_setopt ( $ch, CURLOPT_COOKIEFILE, $COOKIE_FILE_PATH ) ;
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ) ;
	curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" ) ;
	if ($POST == false) {
		curl_setopt ( $ch, CURLOPT_POST, 0 ) ;
	} else {
		curl_setopt ( $ch, CURLOPT_POST, 1 ) ;
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $POST ) ;
	}
	curl_setopt ( $ch, CURLOPT_PROXY, '213.132.44.92:80' ) ; //proxy	$data = curl_exec ( $ch ) ;
	curl_close ( $ch ) ;
	$data = str_replace ( 'src="/', 'src="http://brokercheck.finra.org/', $data ) ;
	$data = str_replace ( 'action="TermsAndConditions.aspx"', 'action="http://127.0.0.1/BRO/test.php"', $data ) ;
	$data = str_replace ( 'action="Search.aspx?PageID=1"', 'action="http://127.0.0.1/BRO/test.php"', $data ) ;
	$data = str_replace ( 'action="Search.aspx?PageID=2"', 'action="http://127.0.0.1/BRO/test.php"', $data ) ;
	$data = str_replace ( 'action="SearchResults.aspx"', 'action="http://127.0.0.1/BRO/test.php"', $data ) ;
	return $data ;
}
function GetBetween ( $haystack , $start , $end , $debug = false ) {
	$startpos = strpos ( $haystack, $start ) ;
	$haystack2 = substr ( $haystack, $startpos + strlen ( $start ), strlen ( $haystack ) - $startpos + 1 - strlen ( $start ) ) ;
	$endpos = strpos ( $haystack2, $end ) + $startpos + strlen ( $start ) ;
	if ($debug == true) {
		echo "<br />DEBUG: startPos='$startpos', endPos='$endpos', string='$haystack'<br />" ;
	}
	if ($startpos === false)
		return "" ;
	if ($endpos === false)
		return "" ;
	if ($endpos < $startpos)
		return "" ;
	$endpos = $endpos + strlen ( $end ) ;
	$endpos = $endpos - $startpos ;
	$endpos = $endpos - strlen ( $end ) ;
	$tag = substr ( $haystack, $startpos, $endpos ) ;
	$tag = substr ( $tag, strlen ( $start ) ) ;
	return $tag ;
}

function WriteFile ( $file , $content , $type = "a" ) {
	if (! $handle = fopen ( $file, $type )) {
		return "ERROR: cann't open file [$file]" ;
		exit () ;
	}
	if (fwrite ( $handle, $content ) === false) {
		return "ERROR: cann't write to file [$file]" ;
		exit () ;
	}
	return '' ;
	fclose ( $handle ) ;
}

function removeTags ( $s ) {
	return preg_replace ( '/<[^>]*>/', ' ', $s ) ;
}
?>